package com.jtpay.textpay;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;

import android.util.Log;
import com.jtpay.textpay.db.AppidColumns;

import java.util.ArrayList;
import java.util.List;

import static com.jtpay.textpay.db.PayContentProvider.PROVIDER_AUTHORITY;

public class AppidDataProvider {
    public static final String CONTENT_ALI_QR_CODE_URI = "content://" + PROVIDER_AUTHORITY + "/" + AppidColumns.TABLE_NAME_PAY;
    private static Uri mUri = Uri.parse(CONTENT_ALI_QR_CODE_URI);

    public static synchronized boolean isDbNull(Context context) {
        try (Cursor c = context.getContentResolver()
                .query(mUri,
                        new String[]{AppidColumns._ID,
                                AppidColumns.APPID},
                        AppidColumns.APPID + "!=?",
                        new String[]{" "}, "_id DESC")) {

            if (c != null && c.moveToNext()) {
                return false;
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return true;
    }

    public static synchronized List<PayBean> getListWithoutReport(Context context) {
        List<PayBean> list = new ArrayList<>();
        Cursor c = null;
        try {
            c = context.getContentResolver()
                    .query(mUri, null, null,
                            null, "_id DESC");

            while (c != null && c.moveToNext()) {
                PayBean bean = new PayBean();
                bean.setId(c.getInt(c
                        .getColumnIndexOrThrow(AppidColumns._ID)));
                bean.setAppid(c.getString(c
                        .getColumnIndexOrThrow(AppidColumns.APPID)));
                bean.setAppKey(c.getString(c
                        .getColumnIndexOrThrow(AppidColumns.APP_KEY)));
                bean.setAesKey(c.getString(c
                        .getColumnIndexOrThrow(AppidColumns.AES_KEY)));
                bean.setAesV(c.getString(c
                        .getColumnIndexOrThrow(AppidColumns.AES_VECTOR)));
                list.add(bean);
//                update(bean.getId(), TextApplication.getContext());
            }
            return list;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return null;
    }

    /**
     * upload result provider data update db
     * update treated
     */
    private static synchronized void update(int id, Context context) {
        ContentValues values = new ContentValues();
        Uri updateIdUri = ContentUris.withAppendedId(mUri, id);
        context.getContentResolver().update(updateIdUri, values, null, null);
    }

    /**
     * insert new data format model
     */
    public static synchronized void insertDb(String appid, String appKey, String aesKey, String
            aesV, Context context) {
        try {
            ContentValues values = new ContentValues();
            values.put(AppidColumns.APPID, appid);
            values.put(AppidColumns.APP_KEY, appKey);
            values.put(AppidColumns.AES_KEY, aesKey);
            values.put(AppidColumns.AES_VECTOR, aesV);
            context.getContentResolver().insert(mUri, values);
//            Cursor c = context.getContentResolver()
//                    .query(mUri,
//                            new String[]{AppidColumns._ID},
//                            AppidColumns._ID + ">?",
//                            new String[]{String.valueOf(10)}, "_id DESC");
//            if (c != null && c.moveToNext()) {
//                deleteDB(context);
//            }
            Log.i("ddddbbbbb= ", aesKey);
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("ddddbbbbb= ", e.toString());
        }
    }

    private static synchronized void deleteDB(Context context) {
        try {
            context.getContentResolver().delete(mUri, AppidColumns._ID + "=?", new String[]{String.valueOf(1)});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static synchronized void deleteDB(Context context, String appid) {
        try {
            context.getContentResolver().delete(mUri, AppidColumns.APPID + "=?", new String[]{appid});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
